package io.itit.ecp.admin.server.service.impl;

import cn.hutool.core.date.DateUtil;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import io.itit.ecp.admin.server.entity.ConvertStandardCoalCoefficientEntity;
import io.itit.ecp.admin.server.mapper.ConvertStandardCoalCoefficientMapper;
import io.itit.ecp.admin.server.service.IConvertStandardCoalCoefficientService;
import io.itit.ecp.admin.server.service.IEnergyDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：折标准煤系数服务实现类
 * @Date：Create by 2023-11-23 16:28:52
 */
@Service
public class ConvertStandardCoalCoefficientServiceImpl extends JoinServiceImpl<ConvertStandardCoalCoefficientMapper, ConvertStandardCoalCoefficientEntity> implements IConvertStandardCoalCoefficientService {

    @Resource
    private IEnergyDataService energyDataService;

    @Override
    public boolean updateConvertStandardCoalCoefficient(ConvertStandardCoalCoefficientEntity entity) {
        if (updateById(entity)) {
            // 根据能源类型及日期更新数据管理模块中相关数据
            energyDataService.causeFactorUpdate(true, entity.getEnergyTypeId(), entity.getSolidifyDate(), entity.getCoefficient());
            return true;
        }
        return false;
    }

    @Override
    public ConvertStandardCoalCoefficientEntity findFirstByEnergyTypeIdAndSolidifyDate(String energyTypeId, Date solidifyDate) {
        Date start = DateUtil.beginOfMonth(solidifyDate);
        Date end = DateUtil.endOfMonth(solidifyDate);
        return lambdaQuery()
                .eq(ConvertStandardCoalCoefficientEntity::getEnergyTypeId, energyTypeId)
                .ge(ConvertStandardCoalCoefficientEntity::getSolidifyDate, start)
                .le(ConvertStandardCoalCoefficientEntity::getSolidifyDate, end)
                .one();
    }
}
